package com.hisense.settings;

import static com.google.android.tv.settings.TvSettings.System.SCREEN_DIM_TIMEOUT;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static android.provider.Settings.System.SOUND_EFFECTS_ENABLED;
import com.hisense.settings.util.Wrapper;
import com.android.settings.R;
import android.content.ContextWrapper;
import android.content.Context;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.PreferenceManager; 
import android.content.SharedPreferences;

import java.util.List;
import android.util.Log;

import android.content.Intent;
/**
 * Handles settings menu for image settings.  
 *
 * @author huangjiufa
 * modify by libaocheng 2013-06-15
 */
public class HdmiSettings extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener{
	
	private Wrapper wrapper;
   
	private static final String TAG = "HdmiSettings";
	   
	private static final String KEY_HDMI_CONTROL = "hdmi_control";
	private static final int VALUE_ON_HDMICONTROL = 0;

	private static final String KEY_DEVICE_AUTOPOWER = "device_autopower";
	private static final int VALUE_ON_DEVICEAUTOPOWER = 0;

	private static final String KEY_TV_AUTOPOWER = "tv_autopower";
	private static final int VALUE_ON_TVAUTOPOWER = 0;
	
	private static final String KEY_SYSTEM_AUTOCONTROL = "system_audio_control";
	private static final int VALUE_ON_SYSTEMAUTOCONTROL = 0;
	
	private static final String KEY_HDMI_DEVICELIST = "device_list";

	private CheckBoxPreference mHdmiControlPreference;
	private CheckBoxPreference mDeviceAutoPowerPreference;
	private CheckBoxPreference mTVAutoPowerPreference;
	private CheckBoxPreference mSystemAutoPowerPreference;
	private PreferenceScreen mHdmiDeviceNamePreference;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.hdmi_settings);
        mHdmiControlPreference=(CheckBoxPreference)findPreference(KEY_HDMI_CONTROL);
        mHdmiControlPreference.setOnPreferenceChangeListener(this);
        mDeviceAutoPowerPreference=(CheckBoxPreference)findPreference(KEY_DEVICE_AUTOPOWER);
        mDeviceAutoPowerPreference.setOnPreferenceChangeListener(this);
        mTVAutoPowerPreference=(CheckBoxPreference)findPreference(KEY_TV_AUTOPOWER);
        mTVAutoPowerPreference.setOnPreferenceChangeListener(this);
        mSystemAutoPowerPreference=(CheckBoxPreference)findPreference(KEY_SYSTEM_AUTOCONTROL);
        mSystemAutoPowerPreference.setOnPreferenceChangeListener(this);
        mHdmiDeviceNamePreference=(PreferenceScreen)findPreference(KEY_HDMI_DEVICELIST);
        mHdmiDeviceNamePreference.setOnPreferenceChangeListener(this);
		Wrapper.getWrapperInstance().setContext(this);
    }
    @Override
    public void onResume() {
       super.onResume();
       SharedPreferences hdmisettings=PreferenceManager.getDefaultSharedPreferences(this);
       /*Boolean control_hdmi=hdmisettings.getBoolean(KEY_HDMI_CONTROL,false);
       Boolean device_autopower=hdmisettings.getBoolean(KEY_DEVICE_AUTOPOWER,false);
       Boolean tv_autopower=hdmisettings.getBoolean(KEY_TV_AUTOPOWER,false);
       Boolean system_autocontrol=hdmisettings.getBoolean(KEY_SYSTEM_AUTOCONTROL,false);*/
       wrapper = Wrapper.getWrapperInstance();
       int control_hdmi = Wrapper.getWrapperInstance().getHdmiCec();
       int device_autopower = Wrapper.getWrapperInstance().getDeviceAutoPowerOff();
       int tv_autopower = Wrapper.getWrapperInstance().getTvAutoPowerOn();
       int system_autocontrol = Wrapper.getWrapperInstance().getSystemAudioControl();
       if(control_hdmi==0){
    	   //wrapper.setHdmiCec(0);
    	   mHdmiDeviceNamePreference.setEnabled(false);
    	   
//    	   if(mDeviceAutoPowerPreference.isChecked())
 //   		   mDeviceAutoPowerPreference.setChecked(false);
    	       mDeviceAutoPowerPreference.setEnabled(false);
 //   	       if(mTVAutoPowerPreference.isChecked())
 //   	    	   mTVAutoPowerPreference.setChecked(false);
    	       mTVAutoPowerPreference.setEnabled(false);
 //   	       if(mSystemAutoPowerPreference.isChecked())
  //  	    	   mSystemAutoPowerPreference.setChecked(false);
    	       mSystemAutoPowerPreference.setEnabled(false);    	   
       }
       else
       {    	   
    	   //wrapper.setHdmiCec(1);
    	   mHdmiControlPreference.setChecked(true);
    	   mDeviceAutoPowerPreference.setEnabled(true);    	   
    	   mTVAutoPowerPreference.setEnabled(true);
    	   mSystemAutoPowerPreference.setEnabled(true);
    	   mHdmiDeviceNamePreference.setEnabled(true);
       }
       if(device_autopower==1){
    	   wrapper.setDeviceAutoPowerOff(1);
    	   mDeviceAutoPowerPreference.setChecked(true);
       }
       else{
    	   wrapper.setDeviceAutoPowerOff(0);
    	   mDeviceAutoPowerPreference.setChecked(false);
    	   }
       if(tv_autopower==1){
    	   wrapper.setTvAutoPowerOn(1);
    	   mTVAutoPowerPreference.setChecked(true);
       }
       else{
    	   wrapper.setTvAutoPowerOn(0);
    	   mTVAutoPowerPreference.setChecked(false);
       }
       if(system_autocontrol==1){
    //	   if(wrapper.setSystemAudioControl(1)==1)
	wrapper.setSystemAudioControl(1);
	Log.d(TAG, "system_autocontrol==1");
    	   mSystemAutoPowerPreference.setChecked(true);
       }
       else{
    //	   if(wrapper.setSystemAudioControl(0)==0)
	wrapper.setSystemAudioControl(0);
	Log.d(TAG, "system_autocontrol==0");
    	   mSystemAutoPowerPreference.setChecked(false);
       }
       }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        return false;
    }

    public boolean onPreferenceChange(Preference preference, Object objValue) {
        final String key = preference.getKey();
        if(KEY_HDMI_CONTROL.equals(key)){
        	int value = ((Boolean)objValue)? 1 : 0;
        	try{
				Wrapper.getWrapperInstance().setHdmiCec(value);
				int hdmiCecSetting = wrapper.getHdmiCec();
				if(value != hdmiCecSetting)
					return false;
				
        		if(value == 1)
        		{	
        			mDeviceAutoPowerPreference.setEnabled(true);    	   
        	    	mTVAutoPowerPreference.setEnabled(true);
        	    	mSystemAutoPowerPreference.setEnabled(true); 
        	    	mHdmiDeviceNamePreference.setEnabled(true);
					//Wrapper.getWrapperInstance().setHdmiCec(1);
        		}
        		else       		
        		{
                	Log.d(TAG, "hdmi cec off");
 //       			if(mDeviceAutoPowerPreference.isChecked())
//        				mDeviceAutoPowerPreference.setChecked(false);
        			mDeviceAutoPowerPreference.setEnabled(false);
//        	    	if(mTVAutoPowerPreference.isChecked())
 //       	    	    mTVAutoPowerPreference.setChecked(false);
        	    	mTVAutoPowerPreference.setEnabled(false);
 //       	    	if(mSystemAutoPowerPreference.isChecked())
 //       	    	    mSystemAutoPowerPreference.setChecked(false);
        	    	mSystemAutoPowerPreference.setEnabled(false);
        	    	mHdmiDeviceNamePreference.setEnabled(false);

					//Wrapper.getWrapperInstance().setHdmiCec(0);
        		}

				
        	}
        	catch (NumberFormatException e) {
                Log.e(TAG, "could not control hdmi", e);
            }
        	
        }
		else if(KEY_DEVICE_AUTOPOWER.equals(key))
		{
            Log.d(TAG, "hdmi cec:device auto power off:"+(((Boolean)objValue)?1:0));
			Wrapper.getWrapperInstance().setDeviceAutoPowerOff(((Boolean)objValue)?1:0);
		}
		else if(KEY_TV_AUTOPOWER.equals(key))
		{
            Log.d(TAG, "hdmi cec:tv auto power on:"+(((Boolean)objValue)?1:0));
			Wrapper.getWrapperInstance().setTvAutoPowerOn(((Boolean)objValue)?1:0);
		}
		else if(KEY_SYSTEM_AUTOCONTROL.equals(key))
		{
			int newvalue = ((Boolean)objValue)?1:0;
            Log.d(TAG, "hdmi cec:tv auto power on:"+(((Boolean)objValue)?1:0));
			Wrapper.getWrapperInstance().setSystemAudioControl(((Boolean)objValue)?1:0);
			int sacSetting = wrapper.getSystemAudioControl();
			if(newvalue != sacSetting)
				return false;			
		}

        return true;
    }

}
